package com.lry;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * 事务管理器
 */
@Slf4j
public class Tm {

    private List<Rm> rms;
    private String name;
    private Tc tc;

    public Tm(String name,Tc tc,List<Rm> rms){
      this.name = name;
      this.tc = tc;
      this.rms = rms;
    }

    public void business(){
        String xid = tc.globalBegin(this);
        log.info("tm={},business,开启全局事务，xid={}",name,xid);
        try{
            for (Rm rm : rms) {
                rm.business(xid);
            }
        }catch (Exception e){
            tc.globalRollback(xid);
            throw e;
        }
        tc.globalCommit(xid);
    }

}
